import { Axios } from './core/axios.js';
import _default from './default.js';
import mergeConfig from './helpers/merge-config.js';
import { bind, extend } from './utils.js';

/**
 * 创建axios实例
 * @param {*} options 
 * @returns 
 */
const createInstance = (options) => {
  const context = new Axios(options);
  const instance = bind(Axios.prototype.request, context);

  // copy prototype attr
  extend(instance, Axios.prototype, context, { allOwnKeys: true });

  // copy instance attr
  extend(instance, context, null, { allOwnKeys: true });

  // export create
  instance.create = function _createInstance(instanceOptions = {}) {
    const _mergeConfig = mergeConfig(options, instanceOptions);
    console.log('mergeConfig', _mergeConfig)
    return createInstance(_mergeConfig);
  }

  instance.getInstance = function _getInstance() {
    return context;
  }
  return instance;
}
const instance = createInstance(_default); // 默认先创建一个实例
instance.Axios = Axios;

export default instance;
